package com.ngmaster.socket;

import com.ngmaster.socket.connection.NetConnection;
import com.ngmaster.socket.util.HexByteUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

/**
 * 终端
 * */
public class Terminal<T extends ChannelHandlerContext> {

    private static Logger logger = LoggerFactory.getLogger(Terminal.class);

    private boolean isNull;

    private String iccid;

    private NetConnection<T> netConnection;

    /** 该终端的活跃时间戳 */
    private long activeTime;

    private long offlineTime;

    public Terminal() {
    }

    public Terminal(String iccid, NetConnection<T> netConnection) {
        this.iccid = iccid;
        this.netConnection = netConnection;
    }

    public boolean isNull() {
        return isNull;
    }

    public void setNull(boolean aNull) {
        isNull = aNull;
    }

    public String getIccid() {
        return iccid;
    }

    public void setIccid(String iccid) {
        this.iccid = iccid;
    }

    public NetConnection<T> getNetConnection() {
        return netConnection;
    }

    public void setNetConnection(NetConnection<T> netConnection) {
        this.netConnection = netConnection;
    }

    public long getActiveTime() {
        return activeTime;
    }

    public void setActiveTime(long activeTime) {
        this.activeTime = activeTime;
    }

    public long getOfflineTime() {
        return offlineTime;
    }

    public void setOfflineTime(long offlineTime) {
        this.offlineTime = offlineTime;
    }

    public boolean sendMessage(ProtocolMessage message) {
        if (this.getNetConnection() == null) {
            logger.info("[sendMessage] [fail] [connection is not exit!] [message:{}]", message);
            return false;
        }
        try {
            Thread.currentThread().sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ByteBuf out = this.getNetConnection().getChannelHandlerContext().alloc().buffer();
        out.writeBytes(message.getByteArray().getBytes(), 0, message.getByteArray().getWritePos());
        if (logger.isInfoEnabled()) {
            byte[] sendBytes = new byte[message.getByteArray().getWritePos()];
            out.getBytes(0, sendBytes);
            logger.info("[sendMessage] [sendBytes.length:{}] [sendBytes:{}] [16进制:{}] [msg:{}]", new Object[]{
                    sendBytes.length, Arrays.toString(sendBytes), HexByteUtil.byteArrayToHexString(sendBytes), message
            });
        }
        this.getNetConnection().getChannelHandlerContext().channel().writeAndFlush(out);
        return true;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Terminal{");
        sb.append("isNull=").append(isNull());
        sb.append(", iccid='").append(iccid).append('\'');
        sb.append(", netConnection=").append(netConnection);
        sb.append(", activeTime=").append(activeTime);
        sb.append(", offlineTime=").append(offlineTime);
        sb.append('}');
        return sb.toString();
    }
}
